package com.sunny.ynhz.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by huhuan123 on 2018/5/22.
 */

public abstract class CustomLayFragment extends Fragment {
    public boolean isOncreateView = false;
    public boolean isVisibleToUser = false;
    public boolean hasLoaded = false;
    public View mContentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        hasLoaded = false;
        mContentView = inflater.inflate(setLayoutId(), null);
        isOncreateView = true;
        setUpView();
        if (isOncreateView && isVisibleToUser && !hasLoaded) {
            setupData();
        }
        return mContentView;
    }

    public abstract void setUpView();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        this.isVisibleToUser = isVisibleToUser;
        if (isOncreateView && isVisibleToUser && !hasLoaded) {
            setupData();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    public abstract int setLayoutId();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hasLoaded = false;
        isOncreateView = false;
    }

    public abstract void setupData();
}
